package pig.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import pig.model.BuyVO;
import pig.model.CartVO;
import pig.model.CustomerService;
import pig.model.CustomerVO;
import pig.model.TransactionVO;

@Controller
@RequestMapping("/customer")
public class CustomerController {
	
	@Resource(name="customerServiceImpl")
	private CustomerService customerService;
	
	@RequestMapping("/{viewId}_view.do")
	public String showView(@PathVariable("viewId") String viewId) {
		return viewId + "_view.customer";
	}

	/**
	 * 구매자 회원가입 무결성제약조건을 방지하기 위해 redirect 
	 */
	@RequestMapping(value = "customer_register.do", method = RequestMethod.POST)
	public ModelAndView register(CustomerVO customerVO)	throws UnsupportedEncodingException {
		customerService.customerRegister(customerVO);		
		return new ModelAndView("redirect:cRegisterResult.do");
	}

	/**
	 * 구매자 회원가입
	 */
	@RequestMapping("cRegisterResult.do")
	public String registerResult(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		return "index";
	}

	/**
	 * 구매자 회원가입 아이디 중복체크
	 */
	@RequestMapping(value = "cIdcheck.do", method = RequestMethod.POST)
	@ResponseBody
	public int idcheck(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("customerId");
		return customerService.idCheck(id);
	}
	
	/**
	 * 구매자 회원가입 이메일 중복체크
	 */
	@RequestMapping(value = "cEmailcheck.do", method = RequestMethod.POST)
	@ResponseBody
	public int emailCheck(HttpServletRequest request, HttpServletResponse response) {
		String email = request.getParameter("customerEmail");
		return customerService.emailCheck(email);
	}
	/**
	 * 구매자회원정보 수정
	 * 윤미혜
	 */
	@RequestMapping("updateCustomerInfo.do")
	public ModelAndView updateCustomerInfo(HttpServletRequest request, CustomerVO customerVO) {
		customerService.updateCustomerInfo(customerVO);
		return new ModelAndView("update_result.customer");
	}
	
	/**
	 * 구매자가 장바구니에 등록하는 메서드
	 * 김세환 14.11.25
	*/
	@RequestMapping("addCart.do")
	public ModelAndView addCart(HttpServletRequest request,HttpSession session,CartVO cartVO){
	   CustomerVO customerVO=(CustomerVO)session.getAttribute("login");
	   cartVO.setCustomerId(customerVO.getCustomerId());
	   int result=customerService.searchCartItem(cartVO);
	   if(result==1){
		   customerService.updateCartItem(cartVO);
	   }else{
		   customerService.cartAddDeal(cartVO);   
	   }
		return new ModelAndView("redirect:auth_cart_List.do?customerId="+cartVO.getCustomerId());
	   }
	
   /**
    * 구매자가 장바구니 접속시 리스트 출력
    * 김세환 14.11.25
    */
	@RequestMapping("auth_cart_List.do")
	public ModelAndView cartList(String customerId){
		List<CartVO> list=customerService.cartShowDeal(customerId);
		if(list.size()==0){
			return new ModelAndView("cart_List.customer");			   
		}else{
		   return new ModelAndView("cart_List.customer","list",list);			      
		}
	}   
	
	/**
	 * 구매자가 장바구니에서 물품 삭제
	*/
	@RequestMapping("deleteCart.do")
	public ModelAndView deleteCart(CartVO cartVO){
		customerService.deleteCart(cartVO);
		return new ModelAndView("redirect:auth_cart_List.do?customerId="+cartVO.getCustomerId());
	}
	
	/**
	 * 구매자가 장바구니에 있는 물품 결제시
	*/
	@RequestMapping("transaction_ShowInfo.do")
	public ModelAndView transaction_ShowInfo(String customerId){
		int deal = 0;
		boolean flag=false;
		
		List<CartVO> list=customerService.cartShowDeal(customerId);
		for(int i=0; i<list.size();i++){
		  deal= customerService.cartTranSactionCheck(list.get(i).getDealNumber());		
		  if(list.get(i).getCartCount()>deal){
			  customerService.cartTranSactionDel(list.get(i).getDealNumber(), customerId);				  
			  flag=true;
			  break;
		  }
		}
		
		if(flag){
		   return new ModelAndView("transaction_fail.customer");
		}else{
		   List<CartVO> result=customerService.cartShowDeal(customerId);
		   return new ModelAndView("transaction_ShowInfo.customer","list",result);
		}
	}   
	
   /**
    * 물품결제
    */
   @RequestMapping("transaction.do")
   public ModelAndView transaction_check(TransactionVO transactionVO){
	   TransactionVO tvo=customerService.transaction(transactionVO);
	return  new ModelAndView("redirect:transaction_result.do?transactionNumber="+tvo.getTransactionNumber());
   }  
   
   /**
    * 물품결제후 결제화면
    */
   @RequestMapping("transaction_result.do")
   public ModelAndView transactionResult(String transactionNumber){
	   TransactionVO tvo=customerService.transactionResult(transactionNumber);
	   return new ModelAndView("transaction_result_view.customer","result",tvo);
   }
   
   /**
    * 구매자 구매내역 리스트 보기
    */
   @RequestMapping("auth_buyCheck.do")
   public ModelAndView buyCheck(String customerId){
	   List<BuyVO> list=customerService.buyCheck(customerId);
	   return new ModelAndView("customer_buyCheck.customer","list",list);   
   }
   
}
